package com.suixun.util;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 描述赶紧写上
 *
 * @author <a href="mailto:hellmash@gmail.com">hellmash</a>
 * @version create on 2015-04-13 12:48
 */
public class RegexUtil {

    static final String NUMERIC = "^[0-9]*$";
    static final String URL = "\\b((ftp|https?)://[-\\w]+(\\.\\w[-\\w]*)+|(?i:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\\.)+(?-i:com\\b|edu\\b|biz\\b|gov\\b|in(?:t|fo)\\b|mil\\b|net\\b|org\\b|[a-z][a-z]\\b))(:\\d+)?(/[^.!,?;\"'<>()\\[\\]{}\\s\\x7F-\\xFF]*(?:[.!,?]+[^.!,?;\"'<>()\\[\\]{}\\s\\x7F-\\xFF]+)*)?";
    static final String FENQI_URL = "^(http://)?([^\\.]\\.)?mmfenqi.com";
    static final String PHONE = "(\\(\\d{3}\\)|\\d{3,4}-)?\\d{7,8}$";
    static final String CELLPHONE = "0?1[35874]\\d{9}";
    static final String POSTCODE = "^[0-9]\\d{5}$";
    // static final String EMAIL = "^[a-zA-Z0-9_\\-\\.]{1,}@([a-zA-Z0-9_\\-]{1,}\\.[a-zA-Z0-9]{1,}){1,}$";
    static final String EMAIL = "^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$";


    /**
     * 判断content是否合法(用于js判断内容是否合法)
     * @param content
     * @return
     */
    public static boolean isLegal(String content){
        return exsitsAll(listExsits(content, URL), FENQI_URL);
    }

    /**
     * 判断content中存在regex表达(默认Pattern.CASE_INSENSITIVE)的结果
     *
     * @param content
     * @param regex
     * @return
     */
    public static List<String> listExsits(String content, String regex) {
        Pattern p = Pattern.compile(regex, 0);
        Matcher matcher = p.matcher(content);
        List<String> results = new ArrayList<String>();
        String result = null;
        while (matcher.find()) {
            result = matcher.group();
            results.add(result);
        }
        return results;
    }

    /**
     * 判断content中存在regex表达的结果
     *
     * @param content
     * @param regex
     * @param pattern
     * @return
     */
    public static List<String> listExsits(String content, String regex, int pattern) {
        Pattern p = Pattern.compile(regex, pattern);
        Matcher matcher = p.matcher(content);
        List<String> results = new ArrayList<String>();
        String result = null;
        while (matcher.find()) {
            result = matcher.group();
            results.add(result);
        }
        return results;
    }

    /**
     * 判断content中是否存在regex表达(默认Pattern.CASE_INSENSITIVE)
     *
     * @param content
     * @param regex
     * @return
     */
    public static boolean exsit(String content, String regex) {
        return exist(content, regex, 0);
    }

    /**
     * 检查content中是否存在regex表达
     *
     * @param content
     * @param regex
     * @param pattern
     * @return
     */
    public static boolean exist(String content, String regex, int pattern) {
        if (pattern == 0)
            pattern = Pattern.CASE_INSENSITIVE;
        Pattern p = Pattern.compile(regex, pattern);
        Matcher matcher = p.matcher(content);
        return matcher.find();
    }

    /**
     * 返回content中存在regex表达(默认Pattern.CASE_INSENSITIVE)的数量
     *
     * @param content
     * @param regex
     * @return
     */
    public static int countExsits(String content, String regex) {
        Pattern p = Pattern.compile(regex, 0);
        Matcher matcher = p.matcher(content);
        int result= 0;
        while (matcher.find()) {
            matcher.group();
            result++;
        }
        return result;
    }

    /**
     * 返回content中存在regex表达(默认Pattern.CASE_INSENSITIVE)的数量
     *
     * @param contents
     * @param regex
     * @return
     */
    public static int countExsits(List<String> contents, String regex) {
        Pattern p = Pattern.compile(regex, 0);
        Matcher matcher= null;
        int result= 0;
        for (String content : contents) {
            matcher = p.matcher(content);
            if (matcher.find())
                result++;
        }
        return result;
    }

    /**
     * 判断列表contents中都存在regex表达
     *
     * @param contents
     * @param regex
     * @return
     */
    public static boolean exsitsAll(List<String> contents, String regex) {
        return contents.size() == countExsits(contents, regex);
    }

    /**
     * 判断注册邮箱是否正确
     *
     * @param email
     * @return
     */
    public static boolean isValidEmail(String email) {
        if (StringUtils.isNotBlank(email)) {
            return email.matches(EMAIL);
        }
        return false;
    }

    /**
     * 判断手机号码是否正确
     * @param cellphone
     * @return
     */
    public static boolean isValidCellphone(String cellphone) {
        if (StringUtils.isNotBlank(cellphone)) {
            return cellphone.matches(CELLPHONE);
        }
        return false;
    }

    /**
     * @param url
     * @return
     */
    public static boolean isValidUrl(String url) {
        if (StringUtils.isNotBlank(url)) {
            return url.matches(URL);
        }
        return false;
    }

    /**
     * @param url
     * @return
     */
    public static boolean isNumeric(String value) {
        if (StringUtils.isNotBlank(value)) {
            return value.matches(NUMERIC);
        }
        return false;
    }
    /**
     * 
     * <p>Title: checkPrice</p>
     * <p> 功能描述:校验金额 </p>
     * @param price
     * @return
     */
    public static boolean checkPrice(String price) {
    	if (StringUtils.isNotBlank(price)) {
    		return price.matches("^((([1-9]{1}\\d{0,9}))|([0]{1}))((\\.(\\d){2}))?$");
    	}
    	return true;
    }
    /**
     * 
     * <p>Title: checkIMEI</p>
     * <p> 功能描述:校验IMEI </p>
     * @param imei
     * @return
     */
    public static boolean checkIMEI(String imei) {
    	if (StringUtils.isNotBlank(imei)) {
    		return imei.matches("^[0-9]{14,15}");
    	}
    	return true;
    }
    
    public static boolean checkCardId(String cardid) {
    	if (StringUtils.isNotBlank(cardid)) {
    		return cardid.matches("^[0-9]{10}");
    	}
    	return true;
    }
}
